package com.zjy.software.day09;

public class StringDemo {
    public static void main(String[] args) {
        //最简单,最直接的String创建方式,这种方式从常量池里面获取
        String str = "hello";
        String str2 = "hello";
        System.out.println((str == str2));

        //使用new String创建后, 从堆里面获取, 但是堆里面的内容还是从常量池里面获取
        String str3 = new String("hello");

        //false
        System.out.println("str3 == str2 :" + (str3==str2));

        // 只要是字符串常量相连接,那么编译器在编译的时候就会做优化,把他们直接编译成结果,那么这个结果就从常量池里面获取
        String str4="he"+"llo";

        System.out.println("str4 == str :" + (str4==str));

        String tmp="llo";
        String str5 = "he"+tmp;
        System.out.println("str5 == str :" + (str5==str));

        // final修饰符的作用, 可以让变量变成常量, 常量在编译的时候就会被替换成具体的值
        final String tmp1="llo";
        String str6 = "he"+tmp1;
        System.out.println("str6 == str :" + (str6==str));

        //字符串使用==还是有危险的, 还是推荐使用equals方法
        String str7 = "1234567  8abc567123de567  ";
        System.out.println("str.substring(3) = " + str7.substring(4));
        System.out.println("str7.substring(4,6) = " + str7.substring(4, 6));
        System.out.println("str7.toLowerCase() = " + str7.toLowerCase());
        System.out.println("str7.toUpperCase() = " + str7.toUpperCase());
        System.out.println("str7.trim() =" + str7.trim());
        System.out.println("str7.replace(\"123\",\"999\") = " + str7.replace("123", "999"));

        System.out.println("str7.startsWith(\"1123\") = " + str7.startsWith("1123"));
        System.out.println("str7.indexOf(\"123\") = " + str7.indexOf("567"));
        System.out.println("str7.indexOf(\"567\",5) = " + str7.indexOf("567", 5));
        String str8 = "aa,bb,cc,dd,11,22,44,88";
        String[] split = str8.split(",");
        for (String s : split) {
            System.out.println("s = " + s);
        }

        for (char c : str7.toCharArray()) {
            System.out.println("c = " + c);
        }

        //获取字符串的byte数组, 默认是按照utf-8编码
        byte[] bytes = str7.getBytes();
        for (byte b : bytes) {
            System.out.println("b = " + b);
        }
    }

}
